
#include "stdafx.h"

//////////////////////////// recv ////////////////////////////
TEST(sendTest, send_in_inout)
{
    SOCKET sock = INVALID_SOCKET;
    char buf[100];
    int flags = 0x2 | 0x8;
    
    std::fill(buf, buf+100, 'b');
    Header header(GetCurrentThreadId(), "send");
    send_in_inout sendIN(sock, buf, 100, flags);
    send_in_inout sendOUT;

    std::vector<char> buffer;
    Serialize(header, sendIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(sendOUT);

    EXPECT_EQ(header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ(header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ(sendIN.s_, sendOUT.s_);
    if (sendIN.buf_ && sendOUT.buf_)
    {
        int cmp = strncmp (sendIN.buf_, sendOUT.buf_, 100);
        EXPECT_TRUE(!cmp);
    }
    else {
        EXPECT_FALSE(sendIN.buf_);
        EXPECT_FALSE(sendOUT.buf_);
    }
    EXPECT_EQ(sendIN.len_, sendOUT.len_);
    EXPECT_EQ(sendIN.flags_, sendOUT.flags_);
}

TEST(sendTest, send_in_inout_NULL_parameters)
{
    SOCKET sock = INVALID_SOCKET;
    int flags = 0x2 | 0x8;
    
    Header header(GetCurrentThreadId(), "send");
    send_in_inout sendIN(sock, NULL, 0, flags);
    send_in_inout sendOUT;

    std::vector<char> buffer;
    Serialize(header, sendIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(sendOUT);

    EXPECT_EQ(header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ(header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ(sendIN.s_, sendOUT.s_);
    if (sendIN.buf_ && sendOUT.buf_)
    {
        int cmp = strncmp (sendIN.buf_, sendOUT.buf_, 100);
        EXPECT_TRUE(!cmp);
    }
    else {
        EXPECT_FALSE(sendIN.buf_);
        EXPECT_FALSE(sendOUT.buf_);
    }
    EXPECT_EQ(sendIN.len_, sendOUT.len_);
    EXPECT_EQ(sendIN.flags_, sendOUT.flags_);
}

TEST(sendTest, send_out_inout)
{
    int ret = 11;
    
    Header header(GetCurrentThreadId(), "send");
    send_out_inout sendIN(ret, 7);
    send_out_inout sendOUT;

    std::vector<char> buffer;
    Serialize(header, sendIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(sendOUT);

    EXPECT_EQ(header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ(header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ(sendIN.ret_, sendOUT.ret_);
    EXPECT_EQ(sendIN.wsa_last_error_, sendOUT.wsa_last_error_);
}

//TEST(sendTest, send_out_inout_NULL_parameters)
//{
//    
//}

//////////////////////////// sendto ////////////////////////////
TEST(sendtoTest, sendto_in_inout)
{
    SOCKET sock = INVALID_SOCKET;
    char buf[100];
    int flags = 0x2 | 0x8;
    
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(27015);

    int len = sizeof(addr);
    
    std::fill(buf, buf+100, 'b');
    Header header(GetCurrentThreadId(), "sendto");
    sendto_in_inout sendtoIN(sock, buf, 100, flags, (SOCKADDR*)&addr, len);
    sendto_in_inout sendtoOUT;

    std::vector<char> buffer;
    Serialize(header, sendtoIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(sendtoOUT);

    EXPECT_EQ(header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ(header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ(sendtoIN.s_, sendtoOUT.s_);
    if (sendtoIN.buf_ && sendtoOUT.buf_)
    {
        int cmp = strncmp (sendtoIN.buf_, sendtoOUT.buf_, 100);
        EXPECT_TRUE(!cmp);
    }
    else {
        EXPECT_FALSE(sendtoIN.buf_);
        EXPECT_FALSE(sendtoOUT.buf_);
    }
    EXPECT_EQ(sendtoIN.len_, sendtoOUT.len_);
    EXPECT_EQ(sendtoIN.flags_, sendtoOUT.flags_);
    if (sendtoIN.to_ && sendtoOUT.to_)
    {
        EXPECT_EQ (sendtoIN.to_->sa_family, sendtoOUT.to_->sa_family);
        int cmp = strncmp (sendtoIN.to_->sa_data, sendtoOUT.to_->sa_data, 14);
        EXPECT_TRUE(!cmp);
    }
    else {
        EXPECT_FALSE(sendtoIN.to_);
        EXPECT_FALSE(sendtoOUT.to_);
    }
    EXPECT_EQ(sendtoIN.tolen_, sendtoOUT.tolen_);
}

TEST(sendtoTest, sendto_in_inout_NULL_parameters)
{
    SOCKET sock = INVALID_SOCKET;
    char buf[100];
    int flags = 0x2 | 0x8;
    
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(27015);

    int len = sizeof(addr);
    
    std::fill(buf, buf+100, 'b');
    Header header(GetCurrentThreadId(), "sendto");
    //sendto_in_inout sendtoIN(sock, NULL, 100, flags, (SOCKADDR*)&addr, len);
    //sendto_in_inout sendtoIN(sock, buf, 100, flags, NULL, len);
    sendto_in_inout sendtoIN(sock, NULL, 100, flags, NULL, len);
    sendto_in_inout sendtoOUT;

    std::vector<char> buffer;
    Serialize(header, sendtoIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(sendtoOUT);

    EXPECT_EQ(header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ(header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ(sendtoIN.s_, sendtoOUT.s_);
    if (sendtoIN.buf_ && sendtoOUT.buf_)
    {
        int cmp = strncmp (sendtoIN.buf_, sendtoOUT.buf_, 100);
        EXPECT_TRUE(!cmp);
    }
    else {
        EXPECT_FALSE(sendtoIN.buf_);
        EXPECT_FALSE(sendtoOUT.buf_);
    }
    EXPECT_EQ(sendtoIN.len_, sendtoOUT.len_);
    EXPECT_EQ(sendtoIN.flags_, sendtoOUT.flags_);
    if (sendtoIN.to_ && sendtoOUT.to_)
    {
        EXPECT_EQ (sendtoIN.to_->sa_family, sendtoOUT.to_->sa_family);
        int cmp = strncmp (sendtoIN.to_->sa_data, sendtoOUT.to_->sa_data, 14);
        EXPECT_TRUE(!cmp);
    }
    else {
        EXPECT_FALSE(sendtoIN.to_);
        EXPECT_FALSE(sendtoOUT.to_);
    }
    EXPECT_EQ(sendtoIN.tolen_, sendtoOUT.tolen_);
}

TEST(sendtoTest, sendto_out_inout)
{
    int ret = 11;
    
    Header header(GetCurrentThreadId(), "sendto");
    sendto_out_inout sendtoIN(ret, 7);
    sendto_out_inout sendtoOUT;

    std::vector<char> buffer;
    Serialize(header, sendtoIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(sendtoOUT);

    EXPECT_EQ(header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ(header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ(sendtoIN.ret_, sendtoOUT.ret_);
    EXPECT_EQ(sendtoIN.wsa_last_error_, sendtoOUT.wsa_last_error_);
}

//TEST(sendtoTest, sendto_out_inout_NULL_parameters)
//{
//    
//}
